//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "MultiPath.h"
using namespace Cephei::QL::Methods::Montecarlo;
#include <gen/QL/Methods/Montecarlo/Path.h>
#include <gen/QL/TimeGrid.h>
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Montecarlo::CMultiPath::CMultiPath (Cephei::Core::IVector<Cephei::QL::Methods::Montecarlo::IPath^>^ multiPath) 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phMultiPath = NULL;
#endif
        multiPath ->Lock ();
        INativeVector<Cephei::QL::Methods::Montecarlo::IPath^>^ _NCImultiPath = multiPath->getFeature (NativeFeature::Value);
        CPathVector^ _NCmultiPath = safe_cast<CPathVector^>(_NCImultiPath);
        std::vector<QuantLib::Path>& _multiPath = static_cast<std::vector<QuantLib::Path>&> (_NCmultiPath->GetReference ());
        _ppMultiPath = new boost::shared_ptr<QuantLib::MultiPath> (new QuantLib::MultiPath ( _multiPath ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (multiPath != nullptr) multiPath->Unlock();    //not optional
    }
}
Cephei::QL::Methods::Montecarlo::CMultiPath::CMultiPath (UInt64 nAsset, Cephei::QL::ITimeGrid^ timeGrid) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CTimeGrid^ _CtimeGrid;
    try
    {
#ifdef HANDLE
        _phMultiPath = NULL;
#endif
        QuantLib::Size _nAsset = (QuantLib::Size)ValueHelper::Convert (nAsset); //d
        _CtimeGrid = safe_cast<CTimeGrid^> (timeGrid);
        _CtimeGrid->Lock();
        QuantLib::TimeGrid& _timeGrid = static_cast<QuantLib::TimeGrid&> (_CtimeGrid->GetReference ()); 
        _ppMultiPath = new boost::shared_ptr<QuantLib::MultiPath> (new QuantLib::MultiPath ( _nAsset,  _timeGrid ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CtimeGrid != nullptr) _CtimeGrid->Unlock();
    }
}
Cephei::QL::Methods::Montecarlo::CMultiPath::CMultiPath () 
{
	_pSpinlock = new boost::detail::spinlock ();
    try
    {
#ifdef HANDLE
        _phMultiPath = NULL;
#endif
        _ppMultiPath = new boost::shared_ptr<QuantLib::MultiPath> (new QuantLib::MultiPath ( ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Montecarlo::CMultiPath::CMultiPath (boost::shared_ptr<QuantLib::MultiPath>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMultiPath = NULL;
#endif
	_ppMultiPath = &childNative;
    
}
Cephei::QL::Methods::Montecarlo::CMultiPath::CMultiPath (QuantLib::MultiPath& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phMultiPath = NULL;
#endif
	_ppMultiPath = new boost::shared_ptr<QuantLib::MultiPath> (&childNative);
    
    _MultiPathOwner = owner;
    
}

Cephei::QL::Methods::Montecarlo::CMultiPath::CMultiPath (CMultiPath^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMultiPath = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMultiPath = new boost::shared_ptr<QuantLib::MultiPath> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Montecarlo::CMultiPath::CMultiPath (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMultiPath = NULL;
#endif
	if (!t->IsSubclassOf(CMultiPath::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Montecarlo::CMultiPath::CMultiPath (QuantLib::Handle<QuantLib::MultiPath>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMultiPath = &childNative;
	_ppMultiPath = &static_cast<boost::shared_ptr<QuantLib::MultiPath>>(childNative.currentLink());
    
    _MultiPathOwner = owner;
}
Cephei::QL::Methods::Montecarlo::CMultiPath::CMultiPath (QuantLib::Handle<QuantLib::MultiPath> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phMultiPath = &childNative;
	_ppMultiPath = &static_cast<boost::shared_ptr<QuantLib::MultiPath>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Montecarlo::CMultiPath::CMultiPath (QuantLib::MultiPath childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phMultiPath = NULL;
#endif
	_ppMultiPath = new boost::shared_ptr<QuantLib::MultiPath> (new QuantLib::MultiPath (childNative));
    
}
#endif

Cephei::QL::Methods::Montecarlo::CMultiPath::~CMultiPath ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppMultiPath != NULL)
    {
	    delete _ppMultiPath;
        _ppMultiPath = NULL;
    }
}
Cephei::QL::Methods::Montecarlo::CMultiPath::!CMultiPath ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppMultiPath != NULL)
    {
	    delete _ppMultiPath;
    }
}
QuantLib::MultiPath& Cephei::QL::Methods::Montecarlo::CMultiPath::GetReference ()
{
    if (_ppMultiPath == NULL) throw REFNEW NativeNullException ();
	return **_ppMultiPath;
}
boost::shared_ptr<QuantLib::MultiPath>& Cephei::QL::Methods::Montecarlo::CMultiPath::GetShared ()
{
    if (_ppMultiPath == NULL) throw REFNEW NativeNullException ();
	return *_ppMultiPath;
}
QuantLib::MultiPath* Cephei::QL::Methods::Montecarlo::CMultiPath::GetPointer ()
{
    if (_ppMultiPath == NULL) throw REFNEW NativeNullException ();
	return &**_ppMultiPath;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MultiPath>& Cephei::QL::Methods::Montecarlo::CMultiPath::GetHandle ()
{
	if (_phMultiPath == NULL)
	{
		_phMultiPath = new Handle<QuantLib::MultiPath> (*_ppMultiPath);
	}
	return *_phMultiPath;
}
#endif
bool Cephei::QL::Methods::Montecarlo::CMultiPath::HasNative () 
{
	return (_ppMultiPath != NULL);
}

UInt64 Cephei::QL::Methods::Montecarlo::CMultiPath::AssetNumber::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMultiPath)->assetNumber ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Methods::Montecarlo::IPath^ Cephei::QL::Methods::Montecarlo::CMultiPath::At (UInt64 j)
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        QuantLib::Size _j = (QuantLib::Size)ValueHelper::Convert (j); //a
    	QuantLib::Path& _rv = (QuantLib::Path&)(*_ppMultiPath)->at ( _j );   
        Cephei::QL::Methods::Montecarlo::CPath^ _nrv = REFNEW Cephei::QL::Methods::Montecarlo::CPath (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Methods::Montecarlo::CMultiPath::PathSize::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMultiPath)->pathSize ( );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Montecarlo::IMultiPath^ Cephei::QL::Methods::Montecarlo::CMultiPath_Factory::Create (Cephei::Core::IVector<Cephei::QL::Methods::Montecarlo::IPath^>^ multiPath)
{
    return REFNEW CMultiPath ( multiPath);
}
Cephei::QL::Methods::Montecarlo::IMultiPath^ Cephei::QL::Methods::Montecarlo::CMultiPath_Factory::Create (UInt64 nAsset, Cephei::QL::ITimeGrid^ timeGrid)
{
    return REFNEW CMultiPath ( nAsset,  timeGrid);
}
Cephei::QL::Methods::Montecarlo::IMultiPath^ Cephei::QL::Methods::Montecarlo::CMultiPath_Factory::Create ()
{
    return REFNEW CMultiPath ();
}
